#include "edu_umd_ion_wavemeter_server_WavemeterLibrary.h"
#include "wlmData.h"
#include <stdlib.h>

static JavaVM* VirtualMachine = 0;
static jobject JavaObject = 0;
static jmethodID TaskqueueCallbackMethod = 0;
static int debugging = 0;

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved)
{
	VirtualMachine = jvm;
	printf("Loading Dynamic WavemeterLibrary Version 2009-07-22 14:58\n");
	printf("Virtual Machine %p\n", VirtualMachine );
    return JNI_VERSION_1_2;
}

JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *jvm, void *reserved)
{
	printf("Finalizing WavemeterLibrary\n");
	return;
}


/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    testfunction
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_testfunction
  (JNIEnv *env, jobject obj)
{
	return debugging;
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    rand
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_rand
  (JNIEnv *env, jobject obj)
{
	return rand();
}

void __stdcall TaskQueueCallbackProcEx( long Ver, long Mode, long IntVal, double DblVal, long Res1 )
{
	if (VirtualMachine!=0 && JavaObject!=0) {
		JNIEnv *env=0;
		if ((*VirtualMachine)->AttachCurrentThread(VirtualMachine, (void **)&env, NULL)) {
			--debugging;
			return;
		}
		(*env)->CallVoidMethod(env, JavaObject, TaskqueueCallbackMethod,
				(jint) Ver, (jint) Mode, (jint) IntVal, (jdouble) DblVal, (jint) Res1);
		if ((*env)->ExceptionCheck(env)) {
			(*env)->ExceptionClear(env);
		}
	}
}

//(long Ver, long Mode, long IntVal, double DblVal, long Res 1)
/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    EnableJavaCallback
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_EnableJavaCallback
  (JNIEnv *env, jobject obj)
{
	if (JavaObject == 0) {
	    JavaObject = (*env)->NewGlobalRef(env, obj);
	    if (JavaObject == 0) return -1;
	    jclass cls = (*env)->GetObjectClass(env, JavaObject);
	    if (cls == 0) return -2;
	    TaskqueueCallbackMethod = (*env)->GetMethodID(env, cls, "JavaCallback", "(IIIDI)V");
	    if (TaskqueueCallbackMethod==0) return -3;
		return Instantiate(cInstNotification, cNotifyInstallCallbackEx, (long)(&TaskQueueCallbackProcEx), 0 );
	  }
	return 0;
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    DisableJavaCallback
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_DisableJavaCallback
  (JNIEnv *env, jobject obj)
{
	if (JavaObject!=0) {
		Instantiate(cInstNotification, cNotifyRemoveCallback, 0, 0 );
		(*env)->DeleteGlobalRef(env, JavaObject);
		JavaObject=0;
	}
	return 0;
}


/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    Instantiate
 * Signature: (IIII)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_Instantiate
  (JNIEnv *env, jobject obj, jint RFC, jint Mode, jint P1, jint P2)
{
	return Instantiate(RFC,Mode,P1,P2);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    ControlWLM
 * Signature: (III)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_ControlWLM
  (JNIEnv *env, jobject obj, jint Action, jint App, jint Res1)
{
  return ControlWLM(Action,App,Res1);
}


/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    ControlWLMEx
 * Signature: (IIIII)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_ControlWLMEx
  (JNIEnv *env, jobject obj, jint Action, jint App, jint Ver, jint Ex1, jint Ex2)
{
	return ControlWLMEx(Action,App,Ver,Ex1,Ex2);
}


/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetMeasurementDelayMethod
 * Signature: (II)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetMeasurementDelayMethod
  (JNIEnv *env, jobject obj, jint Mode, jint Delay)
{
  return SetMeasurementDelayMethod(Mode, Delay);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetWLMPriority
 * Signature: (III)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetWLMPriority
  (JNIEnv *env, jobject obj, jint PPC, jint Res1, jint Res2)
{
  return SetWLMPriority(PPC, Res1, Res2);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    PresetWLMIndex
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_PresetWLMIndex
  (JNIEnv *env, jobject obj, jint Ver)
{
  return PresetWLMIndex(Ver);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetWLMVersion
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetWLMVersion
  (JNIEnv *env, jobject obj, jint Ver)
{
  return GetWLMVersion(Ver);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetWLMIndex
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetWLMIndex
  (JNIEnv *env, jobject obj, jint Ver)
{
  return GetWLMIndex(Ver);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetWLMCount
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetWLMCount
  (JNIEnv *env, jobject obj, jint V)
{
  return GetWLMCount(V);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetWavelength
 * Signature: (D)D
 */
JNIEXPORT jdouble JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetWavelength
  (JNIEnv *env, jobject obj, jdouble WL)
{
  return GetWavelength(WL);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetWavelength2
 * Signature: (D)D
 */
JNIEXPORT jdouble JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetWavelength2
  (JNIEnv *env, jobject obj, jdouble WL2)
{
  return GetWavelength2(WL2);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetWavelengthNum
 * Signature: (ID)D
 */
JNIEXPORT jdouble JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetWavelengthNum
  (JNIEnv *env, jobject obj, jint num, jdouble WL)
{
  return GetWavelengthNum(num,WL);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetCalWavelength
 * Signature: (ID)D
 */
JNIEXPORT jdouble JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetCalWavelength
  (JNIEnv *env, jobject obj, jint num, jdouble WL)
{
	return GetCalWavelength(num,WL);
}


/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetFrequency
 * Signature: (D)D
 */
JNIEXPORT jdouble JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetFrequency
  (JNIEnv *env, jobject obj, jdouble F)
{
  return GetFrequency(F);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetFrequency2
 * Signature: (D)D
 */
JNIEXPORT jdouble JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetFrequency2
  (JNIEnv *env, jobject obj, jdouble F2)
{
  return GetFrequency2(F2);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetFrequencyNum
 * Signature: (ID)D
 */
JNIEXPORT jdouble JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetFrequencyNum
  (JNIEnv *env, jobject obj, jint num, jdouble F)
{
  return GetFrequencyNum(num,F);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetTemperature
 * Signature: (D)D
 */
JNIEXPORT jdouble JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetTemperature
  (JNIEnv *env, jobject obj, jdouble T)
{
  return GetTemperature(T);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetTemperature
 * Signature: (D)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetTemperature
  (JNIEnv *env, jobject obj, jdouble T)
{
  return SetTemperature(T);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetExposure
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetExposure
  (JNIEnv *env, jobject obj, jint E)
{
  return GetExposure(E);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetExposure
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetExposure
  (JNIEnv *env, jobject obj, jint E)
{
  return SetExposure(E);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetExposure2
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetExposure2
  (JNIEnv *env, jobject obj, jint E2)
{
  return GetExposure2(E2);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetExposure2
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetExposure2
  (JNIEnv *env, jobject obj, jint Ex2)
{
  return SetExposure2(Ex2);
}
/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetExposureNum
 * Signature: (III)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetExposureNum
  (JNIEnv *env, jobject obj, jint num, jint arr, jint E)
{
  return GetExposureNum(num, arr, E);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetExposureNum
 * Signature: (III)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetExposureNum
  (JNIEnv *env, jobject obj, jint num, jint arr, jint E)
{
  return SetExposureNum(num,arr,E);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetExposureMode
 * Signature: (Z)Z
 */
JNIEXPORT jboolean JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetExposureMode
  (JNIEnv *env, jobject obj, jboolean EM)
{
  return GetExposureMode(EM);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetExposureMode
 * Signature: (Z)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetExposureMode
  (JNIEnv *env, jobject obj, jboolean EM)
{
  return SetExposureMode(EM);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetExposureModeNum
 * Signature: (IZ)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetExposureModeNum
  (JNIEnv *env, jobject obj, jint channel, jboolean mode)
{
	return GetExposureModeNum(channel,mode);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetExposureModeNum
 * Signature: (IZ)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetExposureModeNum
  (JNIEnv *env, jobject obj, jint channel, jboolean mode)
{
	return SetExposureModeNum(channel, mode);
}


/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetExposureRange
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetExposureRange
  (JNIEnv *env, jobject obj, jint ER)
{
  return GetExposureRange(ER);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetResultMode
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetResultMode
  (JNIEnv *env, jobject obj, jint RM)
{
  return GetResultMode(RM);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetResultMode
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetResultMode
  (JNIEnv *env, jobject obj, jint RM)
{
  return SetResultMode(RM);
}
/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetRange
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetRange
  (JNIEnv *env, jobject obj, jint R)
{
  return GetRange(R);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetRange
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetRange
  (JNIEnv *env, jobject obj, jint R)
{
  return SetRange(R);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetPulseMode
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetPulseMode
  (JNIEnv *env, jobject obj, jint PM)
{
  return GetPulseMode(PM);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetPulseMode
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetPulseMode
  (JNIEnv *env, jobject obj, jint PM)
{
  return SetPulseMode(PM);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetWideMode
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetWideMode
  (JNIEnv *env, jobject obj, jint WM)
{
  return GetWideMode(WM);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetWideMode
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetWideMode
  (JNIEnv *env, jobject obj, jint WM)
{
  return SetWideMode(WM);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetDisplayMode
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetDisplayMode
  (JNIEnv *env, jobject obj, jint DM)
{
  return GetDisplayMode(DM);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetDisplayMode
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetDisplayMode
  (JNIEnv *env, jobject obj, jint DM)
{
  return SetDisplayMode(DM);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetFastMode
 * Signature: (Z)Z
 */
JNIEXPORT jboolean JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetFastMode
  (JNIEnv *env, jobject obj, jboolean FM)
{
  return GetFastMode(FM);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetFastMode
 * Signature: (Z)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetFastMode
  (JNIEnv *env, jobject obj, jboolean FM)
{
  return SetFastMode(FM);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetSwitcherMode
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetSwitcherMode
(JNIEnv *env, jobject obj, jint SM)
{
	return GetSwitcherMode(SM);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetSwitcherMode
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetSwitcherMode
  (JNIEnv *env, jobject obj, jint SM)
{
  return SetSwitcherMode(SM);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetSwitcherChannel
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetSwitcherChannel
  (JNIEnv *env, jobject obj, jint CH)
{
  return GetSwitcherChannel(CH);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetSwitcherChannel
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetSwitcherChannel
  (JNIEnv *env, jobject obj, jint CH)
{
	return SetSwitcherChannel(CH);
}


/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetSwitcherSignalStates
 * Signature: (III)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetSwitcherSignalStates
(JNIEnv *env, jobject obj, jint Signal, jobject State )
{
	jclass cls = (*env)->GetObjectClass(env, State);
	jfieldID Use_ID = (*env)->GetFieldID(env, cls, "Use", "I");
	jfieldID Show_ID = (*env)->GetFieldID(env, cls, "Show", "I");
	if ( Use_ID==NULL || Show_ID==NULL ) {
		return 0; /* failed to find the field */
	}
	long Use = 0;
	long Show = 0;

	jint Result = GetSwitcherSignalStates(Signal,&Use,&Show);

	(*env)->SetIntField(env, State, Use_ID, Use);
	(*env)->SetIntField(env, State, Show_ID, Show);
	return Result;
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetSwitcherSignalStates
 * Signature: (III)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetSwitcherSignalStates
   (JNIEnv *env, jobject obj, jint Signal, jint Use, jint Show)
{
	return SetSwitcherSignalStates(Signal, Use, Show);
}


/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetSwitcherSignal
 * Signature: (III)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetSwitcherSignal
  (JNIEnv *env, jobject obj, jint Signal, jint Use, jint Show)
{
   return SetSwitcherSignal(Signal, Use, Show);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetAutoCalMode
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetAutoCalMode
  (JNIEnv *env, jobject obj, jint ACM)
{
	return GetAutoCalMode(ACM);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetAutoCalMode
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetAutoCalMode
  (JNIEnv *env, jobject obj, jint ACM)
{
	return SetAutoCalMode(ACM);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetAutoCalSetting
 * Signature: (IIII)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetAutoCalSetting
  (JNIEnv *env, jobject obj,  jint acs, jint val, jint res1, jint res2)
{
	// TODO this is not working
	long myval, myres2;
	return GetAutoCalSetting(acs, &myval, res1, &myres2);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetAutoCalSetting
 * Signature: (IIII)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetAutoCalSetting
  (JNIEnv *env, jobject obj,  jint acs, jint val, jint res1, jint res2)
{
	return SetAutoCalSetting(acs, val, res1, res2);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetActiveChannel
 * Signature: (ILjava/lang/Integer;I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetActiveChannel
  (JNIEnv *env, jobject obj, jint Mode, jobject Port, jint Res1)
{
	long Dummy;  // Do not Use Mode 3 returnvalue in Port is not supported
	return GetActiveChannel(Mode,&Dummy,Res1);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetActiveChannel
 * Signature: (IIII)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetActiveChannel
  (JNIEnv *env, jobject obj, jint Mode, jint Port, jint Ch, jint Res1)
{
	return SetActiveChannel(Mode, Port, Ch, Res1);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetChannelsCount
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetChannelsCount
  (JNIEnv *env, jobject obj, jint C)
{
	return GetChannelsCount(C);
}



/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetOperationState
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetOperationState
  (JNIEnv *env, jobject obj, jint Op)
{
  return GetOperationState(Op);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    Operation
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_Operation
  (JNIEnv *env, jobject obj, jint Op)
{
  return Operation(Op);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetOperationFile
 * Signature: (Ljava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetOperationFile
  (JNIEnv *env, jobject obj, jstring File)
{
  const jbyte* str = (*env)->GetStringUTFChars(env, File, NULL);
  if (str == NULL) {
    return -1; /* OutOfMemoryError already thrown */
  }
  return SetOperationFile( (jbyte*) str);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    Calibration
 * Signature: (IIDI)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_Calibration
  (JNIEnv *env, jobject obj, jint Type, jint Unit, jdouble Value, jint Channel)
{
  return Calibration(Type,Unit,Value,Channel);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    RaiseMeasurementEvent
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_RaiseMeasurementEvent
  (JNIEnv *env, jobject obj, jint Mode)
{
  return RaiseMeasurementEvent(Mode);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    TriggerMeasurement
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_TriggerMeasurement
    (JNIEnv *env, jobject obj, jint Action)
{
  return TriggerMeasurement(Action);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetLinkState
 * Signature: (Z)Z
 */
JNIEXPORT jboolean JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetLinkState
  (JNIEnv *env, jobject obj, jboolean LS)
{
  return GetLinkState(LS);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetLinkState
 * Signature: (Z)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetLinkState
  (JNIEnv *env, jobject obj, jboolean LS)
{
  return SetLinkState(LS);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    LinkSettingsDlg
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_LinkSettingsDlg
  (JNIEnv *env, jobject obj)
{
  LinkSettingsDlg();
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetPatternItemSize
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetPatternItemSize
  (JNIEnv *env, jobject obj, jint Index)
{
  return GetPatternItemSize(Index);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetPatternItemCount
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetPatternItemCount
  (JNIEnv *env, jobject obj, jint Index)
{
  return GetPatternItemCount(Index);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetPattern
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetPattern
  (JNIEnv *env, jobject obj, jint Index)
{
  return GetPattern(Index);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetPatternNum
 * Signature: (II)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetPatternNum
  (JNIEnv *env, jobject obj, jint Chn, jint Index)
{
  return GetPatternNum(Chn,Index);
}


/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetPatternDataNum
 * Signature: (IIJ)I
 */
JNIEXPORT jobject JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetPatternDataNum
  (JNIEnv *env, jobject obj, jobject Pattern )
{
	jshort* ShortBuffer;
	jint* IntBuffer;
	jclass cls = (*env)->GetObjectClass(env, Pattern);
	jfieldID Channel_ID = (*env)->GetFieldID(env, cls, "Channel", "I");
	jfieldID Index_ID = (*env)->GetFieldID(env, cls, "Index", "I");
	jfieldID Length_ID = (*env)->GetFieldID(env, cls, "Length", "I");
	jfieldID ElementSize_ID = (*env)->GetFieldID(env, cls, "ElementSize", "I");
	jfieldID ShortArray_ID = (*env)->GetFieldID(env, cls, "ShortPattern", "[S");
	jfieldID IntArray_ID = (*env)->GetFieldID(env, cls, "IntPattern", "[I");
	jfieldID Average_ID = (*env)->GetFieldID(env, cls, "Average", "I");

	if ( Channel_ID==NULL || Index_ID==NULL || Length_ID==NULL || ElementSize_ID==NULL
			|| ShortArray_ID==NULL || IntArray_ID==NULL || Average_ID==NULL ) {
		return NULL; /* failed to find the field */
	}
	jint Index = (*env)->GetIntField(env, Pattern, Index_ID);
	jint Channel = (*env)->GetIntField(env, Pattern, Channel_ID);
	//jint Average = (*env)->GetIntField(env, Pattern, Average_ID);

	// Get The information from the Wavemeter
	jint count = GetPatternItemCount(Index);
	jint size = GetPatternItemSize(Index);
	(*env)->SetIntField(env, Pattern, Length_ID, count );
	(*env)->SetIntField(env, Pattern, ElementSize_ID, size );

	if (count==0 || size==0 )
		return Pattern;
	if (size==2)
	{
		ShortBuffer = (jshort*) malloc( sizeof(jshort)*count );
		GetPatternDataNum(Channel, Index, (long) ShortBuffer);
		jshortArray ShortArray;
		ShortArray = (*env)->NewShortArray(env, count );
		if (ShortArray==0) { free(ShortBuffer); return NULL; }
		(*env)->SetShortArrayRegion(env, ShortArray, 0,
									count, ShortBuffer );
		free( ShortBuffer );
		(*env)->SetObjectField(env, Pattern, ShortArray_ID, ShortArray );
	}
	else if (size==4)
	{
		IntBuffer = (jint*) malloc( sizeof(jint)*count );
		GetPatternDataNum(Channel, Index, (long) IntBuffer);
		jintArray IntArray;
		IntArray = (*env)->NewIntArray(env, count );
		if (IntArray==0) { free(IntBuffer); return NULL; }
		(*env)->SetIntArrayRegion(env, IntArray, 0,
								  count, IntBuffer );
		free( IntBuffer );
		(*env)->SetObjectField(env, Pattern, IntArray_ID, IntArray );
	}
	else return Pattern;
	(*env)->SetIntField(env, Pattern, Average_ID, Channel );
	return Pattern;
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetPattern
 * Signature: (II)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetPattern
  (JNIEnv *env, jobject obj, jint Index, jint iEnable)
{
  return SetPattern(Index,iEnable);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetPatternData
 * Signature: (IJ)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetPatternData
  (JNIEnv *env, jobject obj, jint Index, jlong PArray)
{
  return SetPatternData(Index,PArray);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetAnalysisMode
 * Signature: (Z)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetAnalysisMode
  (JNIEnv *env, jobject obj, jboolean am)
{
	return GetAnalysisMode(am);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetAnalysisMode
 * Signature: (Z)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetAnalysisMode
  (JNIEnv *env, jobject obj, jboolean am)
{
	return SetAnalysisMode(am);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetAnalysisItemSize
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetAnalysisItemSize
  (JNIEnv *env, jobject obj, jint Index)
{
  return GetAnalysisItemSize(Index);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetAnalysisItemCount
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetAnalysisItemCount
  (JNIEnv *env, jobject obj, jint Index)
{
  return GetAnalysisItemCount(Index);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetAnalysis
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetAnalysis
  (JNIEnv *env, jobject obj, jint Index)
{
  return GetAnalysis(Index);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetAnalysisData
 * Signature: (IJ)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetAnalysisData
  (JNIEnv *env, jobject obj, jint Index, jlong PArray)
{
  return GetAnalysisData(Index,PArray);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetAnalysis
 * Signature: (II)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetAnalysis
  (JNIEnv *env, jobject obj, jint Index, jint iEnable)
{
  return SetAnalysis(Index,iEnable);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetLinewidth
 * Signature: (ID)D
 */
JNIEXPORT jdouble JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetLinewidth
  (JNIEnv *env, jobject obj, jint Index, jdouble LW)
{
  return GetLinewidth(Index,LW);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetMinPeak
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetMinPeak
  (JNIEnv *env, jobject obj, jint M1)
{
  return GetMinPeak(M1);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetMinPeak2
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetMinPeak2
  (JNIEnv *env, jobject obj, jint M2)
{
  return GetMinPeak2(M2);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetMaxPeak
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetMaxPeak
  (JNIEnv *env, jobject obj, jint M1)
{
  return GetMaxPeak(M1);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetMaxPeak2
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetMaxPeak2
  (JNIEnv *env, jobject obj, jint M2)
{
  return GetMaxPeak2(M2);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetAvgPeak
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetAvgPeak
  (JNIEnv *env, jobject obj, jint A1)
{
  return GetAvgPeak(A1);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetAvgPeak2
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetAvgPeak2
  (JNIEnv *env, jobject obj, jint A2)
{
  return GetAvgPeak2(A2);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetAvgPeak
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetAvgPeak
  (JNIEnv *env, jobject obj, jint PA)
{
  return SetAvgPeak(PA);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetAmplitudeNum
 * Signature: (III)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetAmplitudeNum
  (JNIEnv *env, jobject obj, jint num, jint Index, jint A)
{
  return GetAmplitudeNum(num,Index,A);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetIntensityNum
 * Signature: (ID)D
 */
JNIEXPORT jdouble JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetIntensityNum
  (JNIEnv *env, jobject obj, jint num, jdouble I)
{
  return GetIntensityNum(num,I);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetPowerNum
 * Signature: (JD)D
 */
JNIEXPORT jdouble JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetPowerNum
  (JNIEnv *env, jobject obj, jint num, jdouble p)
 {
	 return GetPowerNum(num,p);
 }


/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetDelay
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetDelay
  (JNIEnv *env, jobject obj, jint D)
{
  return GetDelay(D);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetDelay
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetDelay
  (JNIEnv *env, jobject obj, jint D)
{
  return SetDelay(D);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetShift
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetShift
  (JNIEnv *env, jobject obj, jint S)
{
  return GetShift(S);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetShift
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetShift
  (JNIEnv *env, jobject obj, jint S)
{
  return SetShift(S);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetShift2
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetShift2
  (JNIEnv *env, jobject obj, jint S2)
{
  return GetShift2(S2);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetShift2
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetShift2
  (JNIEnv *env, jobject obj, jint S2)
{
  return SetShift2(S2);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetDeviationMode
 * Signature: (Z)Z
 */
JNIEXPORT jboolean JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetDeviationMode
  (JNIEnv *env, jobject obj, jboolean DM)
{
  return GetDeviationMode(DM);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetDeviationMode
 * Signature: (Z)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetDeviationMode
  (JNIEnv *env, jobject obj, jboolean DM)
{
  return SetDeviationMode(DM);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetDeviationReference
 * Signature: (D)D
 */
JNIEXPORT jdouble JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetDeviationReference
  (JNIEnv *env, jobject obj, jdouble DR)
{
  return GetDeviationReference(DR);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetDeviationReference
 * Signature: (D)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetDeviationReference
  (JNIEnv *env, jobject obj, jdouble DR)
{
  return SetDeviationReference(DR);
}


/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetPIDCourse
 * Signature: (Ljava/lang/String;)I
 */
JNIEXPORT jstring JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetPIDCourse
  (JNIEnv *env, jobject obj)
{
  char buffer[1024];
  GetPIDCourse(buffer);
  return (*env)->NewStringUTF(env, buffer);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetPIDCourse
 * Signature: (Ljava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetPIDCourse
  (JNIEnv *env, jobject obj, jstring PIDC)
{
  const jbyte* str = (*env)->GetStringUTFChars(env, PIDC, NULL);
  if (str == NULL) {
    return -1; /* OutOfMemoryError already thrown */
  }
  jint Result = SetPIDCourse( (jbyte*) str );
  (*env)->ReleaseStringUTFChars(env, PIDC, str);
  return Result;
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetPIDCourseNum
 * Signature: (ILjava/lang/String;)I
 */
JNIEXPORT jstring JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetPIDCourseNum
  (JNIEnv *env, jobject obj, jint port)
{
	char buffer[1024];
	GetPIDCourseNum(port, buffer);
	return (*env)->NewStringUTF(env, buffer);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetPIDCourseNum
 * Signature: (ILjava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetPIDCourseNum
  (JNIEnv *env, jobject obj, jint port, jstring PIDC)
{
	  const jbyte* str = (*env)->GetStringUTFChars(env, PIDC, NULL);
	  if (str == NULL) {
	    return -1; /* OutOfMemoryError already thrown */
	  }
	  jint Result = SetPIDCourseNum( port, (jbyte*) str );
	  (*env)->ReleaseStringUTFChars(env, PIDC, str);
	  return Result;
}


/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetPIDSetting
 * Signature: ()Ledu/umd/ion/server/PIDSetting;
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetPIDSetting
(JNIEnv *env, jobject obj, jobject PIDSetting)
{
  jfieldID PS_ID, Port_ID, iSet_ID, dSet_ID;
  jclass cls = (*env)->GetObjectClass(env, PIDSetting);
  PS_ID = (*env)->GetFieldID(env, cls, "PS", "I");
  Port_ID =  (*env)->GetFieldID(env, cls, "Port", "I");
  iSet_ID =  (*env)->GetFieldID(env, cls, "iSet", "I");
  dSet_ID =  (*env)->GetFieldID(env, cls, "dSet", "D");
  if (PS_ID == NULL || Port_ID==NULL || iSet_ID==NULL || dSet_ID==NULL ) {
    return -1; /* failed to find the field */
  }
  long PS = (*env)->GetIntField(env, PIDSetting, PS_ID );
  long Port = (*env)->GetIntField(env, PIDSetting, Port_ID );
  long iSet = 0;
  double dSet = 0;

  jint Result =  GetPIDSetting(PS,Port,&iSet,&dSet);
  (*env)->SetIntField(env, PIDSetting, iSet_ID, iSet);
  (*env)->SetDoubleField(env, PIDSetting, dSet_ID, dSet);

  return Result;
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetPIDSetting
 * Signature: (Ledu/umd/ion/server/PIDSetting;)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetPIDSetting
  (JNIEnv *env, jobject obj, jobject PIDSetting)
{
  jfieldID PS_ID, Port_ID, iSet_ID, dSet_ID;
  jclass cls = (*env)->GetObjectClass(env, PIDSetting);
  PS_ID = (*env)->GetFieldID(env, cls, "PS", "I");
  Port_ID =  (*env)->GetFieldID(env, cls, "Port", "I");
  iSet_ID =  (*env)->GetFieldID(env, cls, "iSet", "I");
  dSet_ID =  (*env)->GetFieldID(env, cls, "dSet", "D");
  if (PS_ID == NULL || Port_ID==NULL || iSet_ID==NULL || dSet_ID==NULL ) {
    return -1; /* failed to find the field */
  }
  jint PS = (*env)->GetIntField(env, PIDSetting, PS_ID );
  jint Port = (*env)->GetIntField(env, PIDSetting, Port_ID );
  jint iSet = (*env)->GetIntField(env, PIDSetting, iSet_ID );
  jdouble dSet = (*env)->GetDoubleField(env, PIDSetting, dSet_ID );

  return SetPIDSetting(PS,Port,iSet,dSet);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    ClearPIDHistory
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_ClearPIDHistory
  (JNIEnv *env, jobject obj, jint port)
{
	return ClearPIDHistory(port);
}



/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    ConvertUnit
 * Signature: (DII)D
 */
JNIEXPORT jdouble JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_ConvertUnit
  (JNIEnv *env, jobject obj, jdouble Val, jint uFrom, jint uTo)
{
  return ConvertUnit(Val, uFrom, uTo);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    ConvertDeltaUnit
 * Signature: (DDIII)D
 */
JNIEXPORT jdouble JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_ConvertDeltaUnit
  (JNIEnv *env, jobject obj, jdouble Base, jdouble Delta, jint uBase, jint uFrom, jint uTo)
{
  return ConvertDeltaUnit(Base,Delta,uBase,uFrom,uTo);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetDeviationSensitivity
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetDeviationSensitivity
	(JNIEnv *env, jobject obj, jint sense)
{
	return GetDeviationSensitivity(sense);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetDeviationSensitivity
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetDeviationSensitivity
	(JNIEnv *env, jobject obj, jint sense)
{
	return SetDeviationSensitivity(sense);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetDeviationSignal
 * Signature: (D)D
 */
JNIEXPORT jdouble JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetDeviationSignal
	(JNIEnv *env, jobject obj, jdouble Signal)
{
	return GetDeviationSignal(Signal);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetDeviationSignalNum
 * Signature: (ID)D
 */
JNIEXPORT jdouble JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetDeviationSignalNum
	(JNIEnv *env, jobject obj, jint num, jdouble sig)
{
	return GetDeviationSignalNum(num, sig);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetDeviationSignal
 * Signature: (D)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetDeviationSignal
	(JNIEnv *env, jobject obj, jdouble sig)
{
	return SetDeviationSignal(sig);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    SetDeviationSignalNum
 * Signature: (ID)I
 */
JNIEXPORT jint JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_SetDeviationSignalNum
	(JNIEnv *env, jobject obj, jint num, jdouble sig)
{
	return SetDeviationSignalNum(num, sig);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    RaiseDeviationSignal
 * Signature: (ID)D
 */
JNIEXPORT jdouble JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_RaiseDeviationSignal
	(JNIEnv *env, jobject obj, jint i, jdouble d)
{
	return RaiseDeviationSignal(i, d);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetAnalogIn
 * Signature: (D)D
 */
JNIEXPORT jdouble JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetAnalogIn
	(JNIEnv *env, jobject obj, jdouble d)
{
	return GetAnalogIn(d);
}

/*
 * Class:     edu_umd_ion_wavemeter_server_WavemeterLibrary
 * Method:    GetDistance
 * Signature: (D)D
 */
JNIEXPORT jdouble JNICALL Java_edu_umd_ion_wavemeter_server_WavemeterLibrary_GetDistance
	(JNIEnv *env, jobject obj, jdouble d)
{
	return GetDistance(d);
}
